<?php
  require_once("../includes/config.php");
  
    $length = 20;
    
    if( isset($_POST['page'])  && !empty($_POST['page'] ))
    {
      $page =  filter_var($_POST['page'], FILTER_SANITIZE_NUMBER_INT);
      $start = $page*20 - 20;
      unset($_POST['page']);
    }
    else
    {
      $start = 0;
      $page = 1;
    }
   $sql = "SELECT * FROM apartments WHERE owner_id = ? ";
   $sqlCount="SELECT COUNT(*) FROM apartments WHERE owner_id = ? ";
   $trafs=query($sql,$_SESSION['user_id']);
   
    $b= array();
   
   $binds= array();
   $binds['user_id']= $_SESSION['user_id'];
    if(!empty($_POST['payment_name']))
    {
      $binds['payment_name'] = $_POST['payment_name'];
      $sql .=" and apart_name=? ";
	  $sqlCount .= " and apart_name=? "; 
    }
	if(!empty($_POST['floor']))
    {
      $binds['floor'] = $_POST['floor'];
      $sql .=" and floor=? ";
	  $sqlCount .= " and floor=? "; 
    }
	/*if(!empty($_POST['type']))
    {
      $binds['type'] = $_POST['type'];
      $sql .=" and room_type=? ";
	  $sqlCount .= " and room_type=? "; 
    } */
	
	
    $sql .= " LIMIT ?,?";
    $binds['start'] = $start;
    $binds['end'] = $length;
    
    $traffic = query($sql,$binds);
    $sqlCount = query($sqlCount,$binds);
    $totalPages = $sqlCount[0]['COUNT(*)'];
	
 if(isset($_POST['from']) && isset($_POST['to']))
    {
      $binds['from'] = $_POST['from'];
	  $binds['to'] = $_POST['to'];
	  $b['from'] = $_POST['from'];
	  $b['to'] = $_POST['to'];
	  
	  $tota ="SELECT SUM(period) FROM contract_apartments LEFT JOIN 
          contracts ON 
		  contract_apartments.contract_id=contracts.contract_id
		   WHERE contracts.mildai_from between ? AND ? ";
		   $tot=query($tota,$b);
		  // dump($tot[0]['SUM(period)']);
		   render("traffic_reports.php", array("title"=>"تقرير نسبة الاشغال","page"=>$page,"totalPages"=>$totalPages,"traffic"=>$traffic,"start"=>$start,"length"=>$length,"binds"=>$binds,"tot"=>$tot));
    }
	else {
	
    render("traffic_reports.php", array("title"=>"تقرير نسبة الاشغال","page"=>$page,"totalPages"=>$totalPages,"traffic"=>$traffic,"start"=>$start,"length"=>$length,"binds"=>$binds));
}
?>